#!/usr/bin/python

# Print precessed speedup data in Google Code wiki format.

import sys
import os

fileList = os.listdir(sys.argv[1]);

sunspiderFiles = [];
v8Files = [];
richardsFiles = [];
krakenFiles = [];

for file in fileList:
    if file[0] == "s":
        sunspiderFiles.append(file);
    elif file[0] == "v":
        v8Files.append(file);
    elif file[0] == "r":
        richardsFiles.append(file);
    elif file[0] == "k":
        krakenFiles.append(file);

sunspiderTests = [];

for file in sunspiderFiles:
    sunspiderTests = open(sys.argv[1] + file).readlines();

sunspiderTests = [line.split(":", 1)[0] for line in sunspiderTests];

v8Tests = [];

for file in v8Files:
    v8Tests = open(sys.argv[1] + file).readlines();

v8Tests = [line.split(":", 1)[0] for line in v8Tests];

richardsTests = [];

for file in richardsFiles:
    richardsTests = open(sys.argv[1] + file).readlines();

richardsTests = [line.split(":", 1)[0] for line in richardsTests];

krakenTests = [];

for file in krakenFiles:
    krakenTests = open(sys.argv[1] + file).readlines();

krakenTests = [line.split(":", 1)[0] for line in krakenTests];
categories = ["singlebr-bl", "singlebr-bl-ion", "singlebr-bl-ion-inline", "doublebr-bl", "doublebr-bl-ion", "doublebr-bl-ion-inline"];
categoryDic = {"singlebr-bl":"1B + BL", "singlebr-bl-ion":"1B + BL + ION", "singlebr-bl-ion-inline":"1B + BL + ION + INLINING", "doublebr-bl":"2B + BL", "doublebr-bl-ion":"2B + BL + ION", "doublebr-bl-ion-inline":"2B + BL + ION + INLINING"};

# Results.
resultssunspider = [];

for category in categories:
    file = "";

    for f in sunspiderFiles:
        if f.find(category + ".") != -1:
            file = f;

    entries = open(sys.argv[1] + file);
    entries = [entry.split(": ", 1)[-1] for entry in entries];
    entries = [entry.split("\n", 1)[0] for entry in entries];

    resultssunspider.append(entries);

# Results.
resultsv8 = [];

for category in categories:
    file = "";

    for f in v8Files:
        if f.find(category + ".") != -1:
            file = f;

    entries = open(sys.argv[1] + file);
    entries = [entry.split(": ", 1)[-1] for entry in entries];
    entries = [entry.split("\n", 1)[0] for entry in entries];

    resultsv8.append(entries);

# Results.
resultsrichards = [];

for category in categories:
    file = "";

    for f in richardsFiles:
        if f.find(category + ".") != -1:
            file = f;

    entries = open(sys.argv[1] + file);
    entries = [entry.split(": ", 1)[-1] for entry in entries];
    entries = [entry.split("\n", 1)[0] for entry in entries];

    resultsrichards.append(entries);

# Results.
resultskraken = [];

for category in categories:
    file = "";

    for f in krakenFiles:
        if f.find(category + ".") != -1:
            file = f;

    entries = open(sys.argv[1] + file);
    entries = [entry.split(": ", 1)[-1] for entry in entries];
    entries = [entry.split("\n", 1)[0] for entry in entries];

    resultskraken.append(entries);

# Print.
print "|| *" + "SunSpider-1.0" + "* ||"

print ("|| *" + "BENCHMARK\VERSION" + "* ||"),;

for category in categories:
    print(" *" + categoryDic[category] + "* ||"),;

print "";

for test in sunspiderTests:
    print("|| *" + test + "* ||"),
    for category in categories:
        result = resultssunspider[categories.index(category)][sunspiderTests.index(test)];
        
        if result[0] == "0":
            print(" <font color=\"blue\">" + result + "</font> ||"),
        elif result[0] == "-":
            print(" <font color=\"red\">" + result + "</font> ||"),
        else:
            print(" <font color=\"green\">" + result + "</font> ||"),

    print "";

print "";

# Print.
print "|| *" + "v8-v6" + "* ||"

print ("|| *" + "BENCHMARK\VERSION" + "* ||"),;

for category in categories:
    print(" *" + categoryDic[category] + "* ||"),;

print "";

for test in v8Tests:
    print("|| *" + test + "* ||"),
    for category in categories:
        result = resultsv8[categories.index(category)][v8Tests.index(test)];
        
        if result[0] == "0":
            print(" <font color=\"blue\">" + result + "</font> ||"),
        elif result[0] == "-":
            print(" <font color=\"red\">" + result + "</font> ||"),
        else:
            print(" <font color=\"green\">" + result + "</font> ||"),

    print "";

print "";

# Print.
print "|| *" + "JSBench" + "* ||"

print ("|| *" + "BENCHMARK\VERSION" + "* ||"),;

for category in categories:
    print(" *" + categoryDic[category] + "* ||"),;

print "";

for test in richardsTests:
    print("|| *" + test + "* ||"),
    for category in categories:
        result = resultsrichards[categories.index(category)][richardsTests.index(test)];
        
        if result[0] == "0":
            print(" <font color=\"blue\">" + result + "</font> ||"),
        elif result[0] == "-":
            print(" <font color=\"red\">" + result + "</font> ||"),
        else:
            print(" <font color=\"green\">" + result + "</font> ||"),

    print "";

print "";

# Print.
print "|| *" + "kraken-1.1" + "* ||"

print ("|| *" + "BENCHMARK\VERSION" + "* ||"),;

for category in categories:
    print(" *" + categoryDic[category] + "* ||"),;

print "";

for test in krakenTests:
    print("|| *" + test + "* ||"),
    for category in categories:
        result = resultskraken[categories.index(category)][krakenTests.index(test)];
        
        if result[0] == "0":
            print(" <font color=\"blue\">" + result + "</font> ||"),
        elif result[0] == "-":
            print(" <font color=\"red\">" + result + "</font> ||"),
        else:
            print(" <font color=\"green\">" + result + "</font> ||"),

    print "";
